﻿using MediatR;
using MES.Process.API.Application.Command.工序命令;
using MES.Process.Domain;
using MES.Process.Dto;
using MES.Process.ErrorCode;
using MES.Process.Infrastructure;
using Microsoft.EntityFrameworkCore;
using System.Diagnostics;

namespace MES.Process.API.Application.CommandHandler.工序命令处理
{
    /// <summary>
    /// 工序查询命令处理器
    /// </summary>
    public class ProcessDtoQueryCommandHandler : IRequestHandler<ProcessDtoQueryCommand, APIResult<APIPaging<ProcessCartfsDto>>>
    {
        private readonly IBaseRepository<Processes> processesRepository;
        private readonly IBaseRepository<ProcessType> processTypeRepository;
        private readonly ILogger<ProcessAddCommandHandler> logger;
        public ProcessDtoQueryCommandHandler(IBaseRepository<Processes> processesRepository, IBaseRepository<ProcessType> processTypeRepository = null, ILogger<ProcessAddCommandHandler> logger = null)
        {
            this.processesRepository = processesRepository;
            this.processTypeRepository = processTypeRepository;
            this.logger = logger;
        }

        public async Task<APIResult<APIPaging<ProcessCartfsDto>>> Handle(ProcessDtoQueryCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<APIPaging<ProcessCartfsDto>> result = new APIResult<APIPaging<ProcessCartfsDto>>();
                var serchlist = processesRepository.GetValues();
                if (!string.IsNullOrEmpty(request.ProcessCode)) 
                {
                    serchlist = serchlist.Where(x => x.ProcessCode == request.ProcessCode);
                }
                if (!string.IsNullOrEmpty(request.ProcessName)) 
                {
                    serchlist = serchlist.Where(x => x.ProcessName.Contains(request.ProcessName));
                }
                var list = from a in serchlist
                           join b in processTypeRepository.GetValues()
                           on a.PTId equals b.ID
                           select new ProcessCartfsDto
                           {
                               ID = a.ID,
                               ProcessCode = a.ProcessCode,
                               ProcessName = a.ProcessName,
                               ProcessStatus = a.ProcessStatus,
                               IsImportent = a.IsImportent,
                               PTId = a.PTId,
                               Note = a.Note,
                               ResponsibilityName = a.ResponsibilityName,
                               DepartId = a.DepartId,
                               PTCode = b.PTCode,
                               PTName = b.PTName,
                           };
                APIPaging<ProcessCartfsDto> paging = new APIPaging<ProcessCartfsDto>();
                int skip = (request.PageIndex - 1) * request.PageSize;
                paging.TotalCount = await list.CountAsync(cancellationToken);
                paging.List = await list
                    .OrderByDescending(x => x.ID)
                    .Skip(skip)
                    .Take(request.PageSize)
                    .ToListAsync(cancellationToken);
                paging.TotalPages = (int)Math.Ceiling(paging.TotalCount * 1.0 / request.PageSize);
                result.Code = APIEnums.Success;
                result.Message = "获取成功";
                logger.LogInformation("工序列表查询成功");
                result.Data=paging;
                return result;
            }
            catch (Exception ex)
            {

                throw;
            }
        }
    }
}
